package com.ilop.sthome.ui.adapter.device;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.CustomToastUtil;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.data.bean.WarnBean;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.AutomationDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.example.common.utils.DateUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-01-13.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class HistoryDetailAdapter extends RecyclerView.Adapter<HistoryDetailAdapter.ItemHolder> {

    private Context mContext;
    private List<WarnBean> mList;

    HistoryDetailAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<WarnBean> mList) {
        this.mList = mList;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_history_detail, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        WarnBean mWarnBean = mList.get(i);
        if(mWarnBean.getDevice_id()==0){
            String room;
            String name;
            String status;
            DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mWarnBean.getDeviceName());
            if (gateway!=null) {
                if (TextUtils.isEmpty(gateway.getNickName())) {
                    name = mContext.getString(R.string.ali_gateway);
                } else {
                    name = gateway.getNickName();
                }
                if (gateway.getOwned() != 1){
                    room = mContext.getString(R.string.ali_gateway_share);
                }else if (!TextUtils.isEmpty(gateway.getRoom_id())) {
                    RoomBean roomBean = RoomDaoUtil.getInstance().getRoomByRoomId(gateway.getRoom_id());
                    if (roomBean.getNickname() != null){
                        room = roomBean.getNickname();
                    }else {
                        room = ConfigureUtil.roomName(mContext, roomBean.getRid());
                    }
                }else {
                    room = mContext.getString(R.string.default_room);
                }
            }else {
                name = mContext.getString(R.string.ali_gateway);
                room = mContext.getString(R.string.default_room);
            }
            if ("AC".equals(mWarnBean.getType())){
                String mAutoName;
                AutomationBean mAuto = AutomationDaoUtil.getInstance().findAutomationByMid(mWarnBean.getMid(), mWarnBean.getDeviceName());
                if (mAuto != null){
                    List<DeviceInfoBean> mDeviceList = mAuto.getInputList();
                    if (mDeviceList!=null && mDeviceList.size()>0){
                        mAutoName = mContext.getString(SmartDevice.getType(mDeviceList.get(0).getDevice_type()).getTypeStrId()) + "\t-\t";
                    }else {
                        mAutoName = mContext.getString(R.string.automation) + mWarnBean.getMid();
                    }
                }else {
                    mAutoName = mContext.getString(R.string.automation) + mWarnBean.getMid();
                }
                status = mAutoName + mContext.getString(R.string.trigger);
            }else {
                status = DeviceStatusUtil.getGatewayAlert(mContext, mWarnBean.getDevice_status());
            }
            String title = room + "\t\t" +  name + "\t\t" +  status;
            itemHolder.mName.setText(title);
            itemHolder.mImage.setImageResource(R.mipmap.gs198);
        }else {
            DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findByDeviceId(mWarnBean.getDeviceName(), mWarnBean.getDevice_id());
            String subDeviceName;
            String room;
            if (deviceInfoBean!=null){
                if(!TextUtils.isEmpty(deviceInfoBean.getSubdeviceName())){
                    subDeviceName = deviceInfoBean.getSubdeviceName();
                }else {
                    subDeviceName = mContext.getString(SmartDevice.getType(mWarnBean.getDevice_type()).getTypeStrId())+mWarnBean.getDevice_id();
                }
                DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mWarnBean.getDeviceName());
                if (gateway!=null && gateway.getOwned() != 1){
                    room = mContext.getString(R.string.ali_gateway_share);
                }else if (!TextUtils.isEmpty(deviceInfoBean.getRoom_id())) {
                    RoomBean roomBean = RoomDaoUtil.getInstance().getRoomByRoomId(deviceInfoBean.getRoom_id());
                    if (roomBean.getNickname() != null){
                        room = roomBean.getNickname();
                    }else {
                        room = ConfigureUtil.roomName(mContext, roomBean.getRid());
                    }
                }else {
                    room = mContext.getString(R.string.default_room);
                }
            }else {
                subDeviceName = mContext.getString(SmartDevice.getType(mWarnBean.getDevice_type()).getTypeStrId()) + mWarnBean.getDevice_id();
                room = mContext.getString(R.string.default_room);
            }
            String title = room + "\t\t" + subDeviceName +  "\t\t" + DeviceStatusUtil.getAlert(mContext,mWarnBean.getDevice_type(),mWarnBean.getDevice_status());
            itemHolder.mName.setText(title);
            itemHolder.mImage.setImageResource(SmartDevice.getType(mWarnBean.getDevice_type()).getDrawableResId());
        }

        itemHolder.mTime.setText(DateUtil.getDateToString(mWarnBean.getTime()*1000));
        itemHolder.mLineTop.setVisibility(i == 0?View.INVISIBLE:View.VISIBLE);
        itemHolder.mLineBut.setVisibility(i == mList.size()-1?View.INVISIBLE:View.VISIBLE);

        /*itemHolder.itemView.setOnClickListener(v -> {
            DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findByDeviceId(mWarnBean.getDeviceName(), mWarnBean.getDevice_id());
            if (null == deviceInfoBean){
                CustomToastUtil.showToast(mContext, mContext.getString(R.string.lack_of_equipment));
                return;
            }
            //LiveDataBus.get().with(CommonId.ALARM_CLICK).setValue(mWarnBean);
        });*/
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_history_image)
        ImageView mImage;
        @BindView(R.id.tv_history_name)
        TextView mName;
        @BindView(R.id.tv_history_time)
        TextView mTime;
        @BindView(R.id.iv_history_line0)
        View mLineTop;
        @BindView(R.id.iv_history_line)
        View mLineBut;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
